package com.jp.knowledge.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.adapter.ProductDetailAdapter;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.ProductVersion;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiayen     2017-06-12 20:09:02
 */
public class ProductDetailFragment extends BaseFragment {

    private View mView;

    @ViewInject(R.id.can_content_view)
    private RecyclerView recyclerView;

    @ViewInject(R.id.refresh)
    private CanRefreshLayout  refreshLayout;

    private List<ProductVersion>  versions;
    private ProductDetailAdapter adapter;

    private  String id;
    private String platform;
    private int  action;
    public ProductDetailFragment() {
    }

    public  ProductDetailFragment newInstance( String id,String platform){
        this.id=id;
        this.platform=platform;
        return  this;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_product_detail, container, false);
        x.view().inject(this,mView);
        initView();
        return mView;
    }

    private void initView(){
        action = ((Activity)mContext).getIntent().getIntExtra("action",2);
        adapter = new ProductDetailAdapter(mContext,null);
        adapter.setAction(action);
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        refreshLayout.setLoadMoreEnabled(false);
        refreshLayout.setRefreshEnabled(false);
        getData();
    }

    /***
     * 获取数据
     */
    private  void getData(){
        JsonObject param = new JsonObject();
        param.addProperty("id",id);
        param.addProperty("platform",platform);
        HttpHelper.getInstance(mContext).productVersionList(param, 1, new ResultSubscriber.OnResultListener() {
            @Override
            public void onStart(int requestType) {

            }

            @Override
            public void onCompleted(int requestType) {

            }

            @Override
            public void onError(int requestType) {

            }

            @Override
            public void onNext(IModel data, int requestType) {
                if(data.getErrcode()==0) {
                 versions = new ArrayList<ProductVersion>();
                   List<ProductVersion> temp = data.getList(ProductVersion.class);
                    for (ProductVersion  version:temp ) {
                        if(version.getIsGallery()!=0){
                            versions.add(version);
                        }
                    }
                    adapter.updataAdapter(versions);
                }else{
                    ToasUtil.toast(mContext,data.getMessage());
                }
            }
        });
    }
}
